﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace SaaS
{
    public partial class WorkflowLinks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ddlOutput.Items.Count <= 0)
            {
                if (Session["LoginID"] == null)
                    Response.Redirect("UserLogin.aspx");
                else if (Session["WorkflowMode"] == null)
                    Response.Redirect("WorkflowManager.aspx");
                else
                {
                    lblWorkflowName.Text = Session["WorkflowName"].ToString();
                    lblWorkflowID.Text = Session["WorkflowID"].ToString();
                    MiniSaaSDB db = new MiniSaaSDB();
                    SqlDataReader reader;
                    string item;
                    Boolean first = true;
                    db.open();
                    reader = db.executeQuery("SELECT * FROM TENANT_WORKFLOW_SERVICE INNER JOIN SERVICE ON TENANT_WORKFLOW_SERVICE.Service_ID = "
                        + "SERVICE.Service_ID WHERE TENANT_WORKFLOW_SERVICE.Tenant_Workflow_ID = " + lblWorkflowID.Text);
                    while (reader.Read())
                    {
                        item = reader["Order_Number"] + ". " + reader["Service_Function_Name"];
                        ddlOutput.Items.Add(item);
                        if (!first)
                            ddlInput.Items.Add(item);
                        first = false;
                    }
                    db.close();
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ddlOutput.SelectedIndex == (ddlInput.SelectedIndex+1))
            {
                lblError.Text = "Cannot output to input function";
                return;
            }
            else if (ddlOutput.SelectedIndex < 0 || ddlInput.SelectedIndex < 0)
            {
                lblError.Text = "Please select an input and output function";
                return;
            }
            MiniSaaSDB db = new MiniSaaSDB();
            SqlDataReader reader;
            string nextID;
            db.open();
            reader = db.executeQuery("SELECT MAX(Data_Link_ID)+1 FROM DATA_LINK");
            if (!reader.HasRows)
            {
                nextID = "1";
            }
            else
            {
                reader.Read();
                if (reader[0] == null || reader[0].ToString().Equals(""))
                    nextID = "1";
                else
                    nextID = reader[0].ToString();
            }
            db.executeNonQuery("INSERT INTO DATA_LINK (Data_Link_ID, From_Tenant_Workflow_Service_ID, To_Tenant_Workflow_Service_ID)"
                + " VALUES (" + nextID + ", (SELECT Tenant_Workflow_Service_ID FROM TENANT_WORKFLOW_SERVICE WHERE Tenant_Workflow_ID = " +
                lblWorkflowID.Text + " AND Order_Number = " + ddlOutput.Text.Substring(0, ddlOutput.Text.IndexOf(".")) + "), " +
                "(SELECT Tenant_Workflow_Service_ID FROM TENANT_WORKFLOW_SERVICE WHERE Tenant_Workflow_ID = " +
                lblWorkflowID.Text + " AND Order_Number = " + ddlInput.Text.Substring(0, ddlOutput.Text.IndexOf(".")) + "))");
            GridView1.DataBind();
            db.close();
        }
    }
}
